package com.smm.web.vmc.service.impl;

import com.smm.web.vmc.dao.BaseDao;
import com.smm.web.vmc.domain.Receipt;
import com.smm.web.vmc.domain.User;
import com.smm.web.vmc.domain.relation.UserReceiptRelation;
import com.smm.web.vmc.service.UserReceiptRelationService;
import com.smm.web.vmc.service.UserService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zhenghao on 2016/9/9.
 *
 */
@Service
public class UserReceiptRelationServiceImpl implements UserReceiptRelationService {


    @Resource
    private BaseDao<UserReceiptRelation> userReceiptRelationBaseDao;

    @Resource
    private UserService userService;


    @Override
    public List<Receipt> getReceiptByUser(int userId) {

        DetachedCriteria where = DetachedCriteria.forClass(UserReceiptRelation.class);
        where.add(Restrictions.eq("user.id", userId));
        where.addOrder(Order.desc("id"));

        List<UserReceiptRelation> userReceiptRelations = userReceiptRelationBaseDao.query(where);

        if(userReceiptRelations==null || userReceiptRelations.size()==0) return null;

        return userReceiptRelations.stream().map(UserReceiptRelation::getReceipt).collect(Collectors.toList());
    }

    @Override
    public List<User> getUserByReceipt(String repNo) {

        DetachedCriteria where = DetachedCriteria.forClass(UserReceiptRelation.class);
        where.add(Restrictions.eq("receipt.no", repNo));
        where.addOrder(Order.desc("id"));

        List<UserReceiptRelation> userReceiptRelations = userReceiptRelationBaseDao.query(where);

        if(userReceiptRelations==null || userReceiptRelations.size()==0) return null;

        return userReceiptRelations.stream().map(UserReceiptRelation::getUser).collect(Collectors.toList());
    }

    @Override
    public void deleteByUer(int userId) {

        DetachedCriteria where = DetachedCriteria.forClass(UserReceiptRelation.class);
        where.add(Restrictions.eq("user.id", userId));

        List<UserReceiptRelation> userReceiptRelations = userReceiptRelationBaseDao.query(where);

        for(UserReceiptRelation relation:userReceiptRelations){

            userReceiptRelationBaseDao.delete(relation);
        }


    }

    @Override
    public void add(UserReceiptRelation relation) {

        userReceiptRelationBaseDao.add(relation);

    }

    @Override
    public void deleteByReceipt(String repNo) throws Exception {

        DetachedCriteria where = DetachedCriteria.forClass(UserReceiptRelation.class);
        where.add(Restrictions.eq("receipt.no", repNo));

        List<UserReceiptRelation> userReceiptRelations = userReceiptRelationBaseDao.query(where);

        for(UserReceiptRelation relation:userReceiptRelations){

            userReceiptRelationBaseDao.delete(relation);
        }
    }

    @Override
    public UserReceiptRelation getReceiptByUserAndRepNo(int userId, String repNo) {

        DetachedCriteria where = DetachedCriteria.forClass(UserReceiptRelation.class);
        where.add(Restrictions.eq("receipt.no", repNo));
        where.add(Restrictions.eq("user.id", userId));

        List<UserReceiptRelation> userReceiptRelations = userReceiptRelationBaseDao.query(where);

        if(userReceiptRelations==null || userReceiptRelations.size()==0) return null;

        return userReceiptRelations.get(0);
    }
}
